package com.zzu.icukangfu.controller;

import com.jfinal.core.Controller;
import com.zzu.icukangfu.entity.User;
import com.zzu.icukangfu.service.FamilyService;
import com.zzu.icukangfu.service.PatientService;
import com.zzu.icukangfu.service.PhysicianService;
import com.zzu.icukangfu.service.UserService;
import com.zzu.icukangfu.serviceimpl.FamilyServiceImpl;
import com.zzu.icukangfu.serviceimpl.PatientServiceImpl;
import com.zzu.icukangfu.serviceimpl.PhysicianServiceImpl;
import com.zzu.icukangfu.serviceimpl.UserServiceImpl;

public class UserController extends Controller {

	private UserService userService = enhance(UserServiceImpl.class);

	/**
	 * 用户登录，分别 患者 家属 医生 三种身份，其中role为 1 表示患者 2 表示医生 3 表示家属
	 */
	public void login() {
		User user = this.getModel(User.class);
		User u = (User) userService.login(user);
		String role = u.getStr("role");
		if (role.equals("1")) {
			PatientService patientService = enhance(PatientServiceImpl.class);
			setSessionAttr("user", patientService.findByUsername(u.getStr("username")));
			renderJson("list", patientService.findByUsername(u.getStr("username")));
		} else if (role.equals("2")) {
			PhysicianService physicianService = enhance(PhysicianServiceImpl.class);
			setSessionAttr("user", physicianService.findThePhysician(u.getStr("username")));
			renderJson("list", physicianService.findThePhysician(u.getStr("username")));
		} else if (role.equals("3")) {
			FamilyService familyService = enhance(FamilyServiceImpl.class);
			setSessionAttr("user", familyService.findTheFamily(u.getStr("username")));
			renderJson("list", familyService.findTheFamily(u.getStr("username")));
		}
	}

	/**
	 * 用户注册
	 */
	public void register() {
		try {
			User user = this.getModel(User.class);
			UserService userService = enhance(UserServiceImpl.class);
			userService.register(user, getPara("key"));
		} catch (Exception e) {
			renderJson("msg","error");
		}
	}
}
